/*
 * Copyright (C) 2009 Rafael Fernandes
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.phonebooksharing.services.ejb3.facade;

import java.util.ArrayList;
import java.util.Calendar;

import org.apache.commons.lang.math.RandomUtils;

import com.phonebooksharing.dto.friend.ContactItem;
import com.phonebooksharing.dto.friend.GroupItem;
import com.phonebooksharing.dto.gallery.ImageGalleryItem;
import com.phonebooksharing.dto.gallery.VideoGalleryItem;
import com.phonebooksharing.dto.gallery.WishlistItem;

/**
 * Super class for all the facades.<br>
 * It contains helper methods for faking data onto the beans<p>
 * 
 * <b>Important:</b> It isn't a static helper class because we
 * don't wanna locking threads on synchronized methods all the time.
 * Being a super class should avoid this issues.
 * 
 * @author <a href='mailto:luizrafael@gmail.com'>Rafael Fernandes</a>
 *
 */
public abstract class AbstractFacadeBean {
    
    public static final int NUMBER_OF_ITEMS_IN_COLLECTION = 15;
    
    protected void fillWithGroups(ArrayList<GroupItem> groups, int howmany) {
        GroupItem gi;
        ArrayList<ContactItem> contacts;
        for(int i = 0; i < howmany; i++) {
            gi = new GroupItem(i, "Group name is " + i);
            contacts = new ArrayList<ContactItem>();
            
            fillWithContacts(contacts);
            
            gi.contacts = contacts;
            groups.add(gi);
        }
    }
    
    protected void fillWithContacts(ArrayList<ContactItem> contacts) {
        ContactItem contactItem;
        for(int i = 0; i < NUMBER_OF_ITEMS_IN_COLLECTION; i++) {
            contactItem = new ContactItem();
    
            setContactInfo(contactItem, i);
    
            contacts.add(contactItem);
        }
    }
    
    protected void fillWithImages(ArrayList<ImageGalleryItem> photoList) {
        ImageGalleryItem pi;
        long timeInMilis = Calendar.getInstance().getTimeInMillis();
        for(int i = 0; i < NUMBER_OF_ITEMS_IN_COLLECTION; i++) {
            pi                  = new ImageGalleryItem();
            pi.height           = 320;
            pi.imageLatitude    = "37.0625";
            pi.imageLongitude   = "-95.677068";
            pi.photoID          = i;
            pi.width            = 480;
            pi.when             = timeInMilis;
            pi.whereAt          = "Where at: Near to " + i;
            pi.timesViewed      = i;
            pi.title            = "Image short description " + i;
            pi.name             = "Picture name is " + i;
            
            photoList.add(pi);
        }
    }
    
    protected void fillWithVideos(ArrayList<VideoGalleryItem> list) {
        VideoGalleryItem pi;
        long timeInMilis = Calendar.getInstance().getTimeInMillis();
        for(int i = 0; i < NUMBER_OF_ITEMS_IN_COLLECTION; i++) {
            pi                  = new VideoGalleryItem();
            pi.videoLatitude    = "37.0625";
            pi.videoLongitude   = "-95.677068";
            pi.videoID          = i;
            pi.when             = timeInMilis;
            pi.whereAt          = "Where at: Near to " + i;
            pi.timesViewed      = i;
            pi.title            = "Video short description " + i;
            pi.name             = "Video title is " + i;
            pi.videoLength      = "00:30";
            pi.hasThumb         =  false; //if there is one, change to true
            
            list.add(pi);
        }
    }
    
    protected void fillWithWishlist(ArrayList<WishlistItem> list) {
        WishlistItem pi;
        long timeInMilis = Calendar.getInstance().getTimeInMillis();
        for(int i = 0; i < NUMBER_OF_ITEMS_IN_COLLECTION; i++) {
            pi                  = new WishlistItem();
            pi.when             = timeInMilis;
            pi.whereAt          = "Where at: Near to " + i;
            pi.timesViewed      = i;
            pi.prodAuthor       = "eBay";
            pi.prodID           = "250378350439";
            pi.prodImgUrl       = "http://i20.ebayimg.com/01/i/001/34/e7/5510_1.JPG?set_id=800005007";
            pi.prodName         = "Indiana Jones Dvd Adventure Game";
            pi.prodUrl          = "http://rover.ebay.com/rover/1/711-67261-24966-0/2?mpre=http%3A%2F%2Fcgi.ebay.com%2F" +
            		              "Indiana-Jones-DVD-Adventure-Game_W0QQitemZ250378350439QQcmdZViewItem&itemid=250378350439";
            pi.wishlistID       = i;
            pi.when             = timeInMilis;
            
            list.add(pi);
        }
    }

    protected void setContactInfo(ContactItem contactItem, int counter) {
        setContactInfo(contactItem, counter, false);
    }
    
    protected void setContactInfo(ContactItem contactItem, int counter, boolean hasPbsAccount) {
        contactItem.isMyContact = (counter % 2 == 0);
        
        contactItem.name          = "Contact name " + counter;
        contactItem.hasPbsAccount = (counter % 1 == 0) || hasPbsAccount;
        contactItem.primaryEmail  = "primaryEmail" + counter + "@email.ccc";
        contactItem.primaryPhone  = "+890323782999292";
        contactItem.pbsID         = counter * RandomUtils.nextLong() * 98;
        
        if(contactItem.isMyContact) {
            contactItem.andID = 1; //should have at least 1 contact on android, take its ID
        } else {
            //no picture set
        }
        
        if(contactItem.hasPbsAccount) {
            contactItem.location      = "1600 Amphitheatre Parkway, Mountain View, CA 94043";
            contactItem.status        = "Dynamic status " + counter;
            contactItem.longitude     = -95.677068;
            contactItem.latitude      = 37.0625;
            contactItem.distance      = 2;
            contactItem.authItem      = "authItem_" + counter;
            contactItem.imageCount    = counter;
            contactItem.videoCount    = counter;
            contactItem.wishListCount = counter;
            contactItem.contactCount  = counter;
        }
    }
}
